package gitee.cheungming.modules.governance.util;

import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DeviceIdGenerator {
    // 设备类型前缀
    private static final String[] DEVICE_PREFIXES = {
            "CAM", "SEN", "GPS", "DVR", "NVR", "IPC",
            "AP", "SW", "RT", "AC", "IOT", "HVAC"
    };

    // 设备型号后缀
    private static final String[] DEVICE_MODELS = {
            "1000", "2000", "3000", "PRO", "LITE", "MAX",
            "ULTRA", "X", "Z", "TURBO", "PLUS", "NANO"
    };

    // 字符集（大写字母 + 数字，排除易混淆字符）
    private static final char[] CHARS = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ".toCharArray();

    // 生成单个设备号
    public static String generate() {
        Random random = ThreadLocalRandom.current();
        String prefix = DEVICE_PREFIXES[random.nextInt(DEVICE_PREFIXES.length)];
        String model = DEVICE_MODELS[random.nextInt(DEVICE_MODELS.length)];

        // 生成唯一序列号（8位）
        StringBuilder serial = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            serial.append(CHARS[random.nextInt(CHARS.length)]);
        }

        return prefix + "-" + model + "-" + serial;
    }

    // 批量生成设备号（确保唯一）
    public static List<String> generateBatch(int count) {
        return Stream.generate(DeviceIdGenerator::generate)
                .parallel()
                .distinct()
                .limit(count)
                .collect(Collectors.toList());
    }
}
